//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.util;

import java.text.SimpleDateFormat;
import java.util.*;

public class MEDate {

    public MEDate() {
        motifPlain = "dd/MM/yyyy";
        plain = "";
        motifDateTime = "dd/MM/yyyy HH:mm:ss";
        dateTime = "";
        motifDateTimeDB = "yyyy-MM-dd HH:mm:ss";
        dateTimeDB = "";
        motifMysql = "yyyy-MM-dd";
        mysql = "";
        motifNumerique = "yyyyMMdd";
        numerique = "";
        motifMoisAnnee = "MMyy";
        moisAnnee = "";
        motifEBP = "ddMMyy";
        ebp = "";
        motifPrint = "d MMMMMMMMM yyyy";
        print = "";
        shortDate = new SimpleDateFormat();
        printDate = new SimpleDateFormat(motifPrint, Locale.FRANCE);
        initDate();
    }

    public void init() {
        initDate();
    }

    public String getPlain() {
        return plain;
    }

    public String getDateTime() {
        return dateTime;
    }

    public String getDateTimeDB() {
        initDate();
        return dateTimeDB;
    }

    public String getMysql() {
        return mysql;
    }

    public String getNum() {
        return numerique;
    }

    public String getMoisAnnee() {
        return moisAnnee;
    }

    public String getEBP() {
        return ebp;
    }

    public String getDateEndTrial() {
        cal.setTime(new Date());
        cal.add(5, 30);
        shortDate.applyPattern(motifNumerique);
        return shortDate.format(cal.getTime());
    }

    public String getDateAddXDay(String date, int x) {
        StringTokenizer st = new StringTokenizer(date, "/");
        int d = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        cal.set(y, m - 1, d);
        cal.add(5, x);
        shortDate.applyPattern(motifPlain);
        return shortDate.format(cal.getTime());
    }

    public int todayCompareTo(String date) {
        StringTokenizer st = new StringTokenizer(getPlain(), "/");
        int d = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        cal.set(y, m - 1, d);
        Calendar cal2 = Calendar.getInstance();
        st = new StringTokenizer(date, "/");
        d = Integer.parseInt(st.nextToken());
        m = Integer.parseInt(st.nextToken());
        y = Integer.parseInt(st.nextToken());
        cal2.set(y, m - 1, d);
        return cal.compareTo(cal2);
    }

    public int toNDay(String d) {
        int year = Integer.parseInt(d.substring(0, 4));
        int month = Integer.parseInt(d.substring(4, 6));
        int day = Integer.parseInt(d.substring(6));
        cal.set(year, month - 1, day);
        int lastday = cal.get(6);
        cal.setTime(new Date());
        int today = cal.get(6);
        day = lastday - today;
        if (day < 0) {
            day = (365 - today) + lastday;
        }
        return day;
    }

    public String toPrint(String s) {
        if (s.equals("") || s.equals(" ")) {
            print = "";
        } else {
            print = printDate(s);
        }
        return print;
    }

    public String toMysql(String s) {
        if (s == null || s.equals("") || s.equals(" ") || s.equals("00/00/0000")) {
            print = "";
        } else {
            print = mysqlDate(s);
        }
        return print;
    }

    public String toME(String s) {
        if (s == null || s.equals("") || s.equals(" ") || s.equals("0000-00-00")) {
            print = "";
        } else {
            print = meDate(s);
        }
        return print;
    }

    public String firstDateOfTheWeek() {
        cal.setTime(new Date());
        cal.set(7, 2);
        shortDate.applyPattern(motifPlain);
        return shortDate.format(cal.getTime());
    }

    public String lastDateOfTheWeek() {
        cal.setTime(new Date());
        cal.set(7, 7);
        shortDate.applyPattern(motifPlain);
        return shortDate.format(cal.getTime());
    }

    private void initDate() {
        cal = Calendar.getInstance();
        shortDate.applyPattern(motifPlain);
        plain = shortDate.format(cal.getTime());
        shortDate.applyPattern(motifDateTime);
        dateTime = shortDate.format(cal.getTime());
        shortDate.applyPattern(motifDateTimeDB);
        dateTimeDB = shortDate.format(cal.getTime());
        shortDate.applyPattern(motifMysql);
        mysql = shortDate.format(cal.getTime());
        shortDate.applyPattern(motifNumerique);
        numerique = shortDate.format(cal.getTime());
        shortDate.applyPattern(motifMoisAnnee);
        moisAnnee = shortDate.format(cal.getTime());
        shortDate.applyPattern(motifEBP);
        ebp = shortDate.format(cal.getTime());
    }

    private String printDate(String s) {
        StringTokenizer st = new StringTokenizer(s, "/");
        int d = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        cal.set(y, m - 1, d);
        s = printDate.format(cal.getTime());
        return s;
    }

    private String mysqlDate(String s) {
        StringTokenizer st = new StringTokenizer(s, "/");
        int d = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int y = Integer.parseInt(st.nextToken());
        cal.set(y, m - 1, d);
        shortDate.applyPattern(motifMysql);
        s = shortDate.format(cal.getTime());
        return s;
    }

    private String meDate(String s) {
        StringTokenizer st = new StringTokenizer(s, "-");
        int y = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());
        int d = Integer.parseInt(st.nextToken());
        cal.set(y, m - 1, d);
        shortDate.applyPattern(motifPlain);
        s = shortDate.format(cal.getTime());
        return s;
    }
    private Calendar cal;
    private SimpleDateFormat shortDate;
    private SimpleDateFormat printDate;
    private String motifPlain;
    private String plain;
    private String motifDateTime;
    private String dateTime;
    private String motifDateTimeDB;
    private String dateTimeDB;
    private String motifMysql;
    private String mysql;
    private String motifNumerique;
    private String numerique;
    private String motifMoisAnnee;
    private String moisAnnee;
    private String motifEBP;
    private String ebp;
    private String motifPrint;
    private String print;
}
